package com.cskaoyan.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author stone
 * @date 2022/04/30 14:53
 */
@RestController
@RequestMapping("other")
public class OtherParameterController {

    //这两个参数基本上不会出现在Handler方法的形参上，因为你要使用request和response要做的事情都有方便的使用方式
    @RequestMapping("reqAndResp")
    public String reqAndResp(HttpServletRequest request, HttpServletResponse response) {

        return "ok";
    }

    @RequestMapping("cookie")
    public String cookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println("cookie.getName() = " + cookie.getName());
            System.out.println("cookie.getValue() = " + cookie.getValue());
            System.out.println("*****");
        }
        return "ok";
    }

    //雪茄
    //localhost:8080/other/session1?username=xuejia
    @RequestMapping("session1")
    public String session1(HttpServletRequest request,String username) {
        HttpSession session = request.getSession();
        session.setAttribute("username", username);
        return "ok";
    }

    //localhost:8080/other/session2
    @RequestMapping("session2")
    public String session2(HttpSession session) {
        Object username = session.getAttribute("username");
        System.out.println("username = " + username);
        return "ok";
    }


}
